/*
* 设置配合比
* */

import 'package:center_beijing/api/product_api.dart';
import 'package:center_beijing/common/numLengthInputFormatter.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/widgets/custom_input_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class MixProportion extends StatefulWidget {
  @override
  _MixProportionState createState() => _MixProportionState();
}

class _MixProportionState extends State<MixProportion> {
  List comList = Get.arguments;

  List dataSource = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.getProportion();
  }

  getProportion() async {
    var pass = {'mixProportionId': comList[0]['mixProportion']['id']};
    var result = await getMixProportionList(pass);

    if (result != null && result.length > 0) {
      this.setState(() {
        dataSource = result;
      });
    } else {}
  }

  submit(pass) async {
    var result = await addMixProportion(pass);

    if (result != null) {
      Fluttertoast.showToast(msg: '配置成功');
      Get.back();
    } else {

    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('设置配合比'),
        ),
        body: SafeArea(
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 0, 0, 70),
            child: SingleChildScrollView(
              child: Column(
                children: dataSource.map((e) {
                  return Container(
                    padding: EdgeInsets.all(Styles.spacing8),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: Text(
                            "${e['bomName'] ?? '--'}(${e['unit'] ?? '--'})",
                            style: TextStyle(
                                color: Styles.DetailColor,
                                fontSize: Styles.NormalFont),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.fromLTRB(0, Styles.spacing4, 0, 0),
                          child: CustomInput(
                            textAlign: TextAlign.left,
                            keyboardType:
                                TextInputType.numberWithOptions(decimal: true),
                            inputformat: [
                              LengthLimitingTextInputFormatter(10),
                              NumLengthInputFormatter(digit: 2)
                            ],
                            borderWidth: 1,
                            backColor: Styles.GrayLight,
                            title: '',
                            placeHold: '请输入',
                            clearAll: false,
                            require: false,
                            readOnly: false,
                            defaultValue: e['quantity'].toString(),
                            textChange: (value) {
                              e['quantity'] = value;
                            },
                          ),
                        ),
                      ],
                    ),
                  );
                }).toList(),
              ),
            ),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Container(
          padding: EdgeInsets.fromLTRB(
              Styles.spacing8, Styles.spacing8, Styles.spacing8, 10),
          child: Container(
            decoration: new BoxDecoration(
              //背景
              color: Styles.BlueColor,
              //设置四周圆角 角度
              borderRadius: BorderRadius.all(Radius.circular(12.0)),
            ),
            child: FlatButton(
              disabledColor: Styles.SelectColor,
              onPressed: () {
                FocusScope.of(context).requestFocus(new FocusNode());
                List pass = [];
                this.dataSource.forEach((element) {
                  element['componentId'] = '';
                  if (element['quantity'] == null ||
                      element['quantity'] == '') {
                    Fluttertoast.showToast(msg: '输入值不能为空');
                    return;
                  }
                  this.comList.forEach((element2) {
                    var bbq = Map.from(element);
                    bbq['componentId'] = element2['id'];
                    pass.add(bbq);
                  });
                });

                print(pass);

                showDialog(
                    barrierDismissible: false,
                    context: this.context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('注意'),
                        content: Text('确认提交吗？'),
                        actions: <Widget>[
                          new Container(
                            child: FlatButton(
                              child: new Text(
                                "取消",
                                style: TextStyle(color: Colors.black),
                              ),
                              onPressed: () {
                                Navigator.pop(context);
                              },
                            ),
                          ),
                          new Container(
                            child: FlatButton(
                              child: new Text(
                                "确定",
                                style: TextStyle(color: Colors.red),
                              ),
                              onPressed: () {
                                Navigator.pop(context);
                                this.submit(pass);
                              },
                            ),
                          ),
                        ],
                      );
                    });
              },
              child: Container(
                height: Styles.MainButtonHeight,
                alignment: Alignment.center,
                child: Text(
                  '确定',
                  style: TextStyle(
                      fontSize: Styles.MainButtonTextFont, color: Colors.white),
                ),
              ),
            ),
          ),
        ));
  }
}
